package tq.com.tqteacher.mine.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import org.json.JSONException;
import org.json.JSONObject;

import tq.com.systools.system.AlertDialogHelper;
import tq.com.tqcom.base.BaseRecyclerFragment;
import tq.com.tqcom.net.INetCallback;
import tq.com.tqcom.net.TQNetResult;
import tq.com.tqcom.util.TQSystemDefine;
import tq.com.tqteacher.mine.adapter.TQClassInfoAdapter;
import tq.com.tqteacher.model.TQLogin;
import tq.com.tqteacher.net.TQNetInterface;

public class TQClassInfoFragment extends BaseRecyclerFragment {
    private TQClassInfoAdapter mClassInfoAdapter;
    @Override
    protected void initRecycleView(RecyclerView pRecyclerView) {
        super.initRecycleView(pRecyclerView);

        pRecyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        if(mClassInfoAdapter == null){
            mClassInfoAdapter = new TQClassInfoAdapter(getContext());
        }
        pRecyclerView.setAdapter(mClassInfoAdapter);

        loadData();
    }

    private void loadData(){
        TQNetInterface.getClsInfo(String.valueOf(TQLogin.getCurrent().getUserInfo().getClzid()), new INetCallback() {
            @Override
            public void result(TQNetResult pNetResult) {
                if(!pNetResult.isSuccess()){
                    AlertDialogHelper.show(TQClassInfoFragment.this.getContext(), pNetResult.getErrMsg());
                    return;
                }

                if(pNetResult.getData() == null){
                    return;
                }

                String[] info = new String[5];
                try {
                    JSONObject cls = pNetResult.getData().getJSONObject("class");
                    info[0] = cls.optString("pic");
                    info[1] = cls.optString("name");
                    info[2] = cls.optString("city");
                    info[3] = TQSystemDefine.gradeName(cls.optInt("grade", TQLogin.getCurrent().getUserInfo().getGrade()));
                    info[4] = cls.optString("clzno");

                    mClassInfoAdapter.bindInfo(info);
                }catch (JSONException pE){
                    pE.printStackTrace();
                }
            }
        });
    }
}
